<?php
namespace App\Modules\User\Models;

use App\Exceptions\StandardException;
use App\Library\Lock;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
 * Class Account
 * @package App\Modules\User\Models
 * @property integer $income
 * @property integer $money
 * @property integer $outlay
 */
class Account extends Model
{
    public $incrementing = false;

    protected $fillable = [
        'id',
        'income',
        'money',
        'outlay',
    ];

    public function incrementMoney(int $money, string $from, string $title, string $remark = null)
    {
        if($money <= 0) {
            throw new StandardException('金额不合法');
        }
        return $this->getLock(function () use($money, $from, $title, $remark) {
            $this->refresh();
            return DB::transaction(function () use($money, $from, $title, $remark) {
                $this->money += $money;
                $this->income += $money;
                $this->save();
                AccountDetail::query()->create([
                    "user_id" => $this->id,
                    "title" => $title,
                    "from" => $from,
                    "to" => AccountDetail::MAIN_BODY_ACCOUNT,
                    "money" => $money,
                    "account" => $this->money,
                    "remark" => $remark,
                ]);
            });
        });
    }

    public function decreaseMoney(int $money, string $to, string $title, string $remark = null)
    {
        if($money <= 0) {
            throw new StandardException('金额不合法');
        }
        return $this->getLock(function () use($money, $to, $title, $remark) {
            $this->refresh();
            if ($money > $this->money) {
                throw new StandardException('提现金额大于余额');
            }
            return DB::transaction(function () use($money, $to, $title, $remark) {
                $this->money -= $money;
                $this->outlay += $money;
                $this->income -= $money;
                $this->save();
                AccountDetail::query()->create([
                    "user_id" => $this->id,
                    "title" => $title,
                    "from" => AccountDetail::MAIN_BODY_ACCOUNT,
                    "to" => $to,
                    "money" => $money,
                    "account" => $this->money,
                    "remark" => $remark,
                ]);
            });
        });
    }

    public function outlayFail(int $money, string $from, string $title, string $remark = null)
    {
        if($money <= 0) {
            throw new StandardException('金额不合法');
        }
        return $this->getLock(function () use($money, $from, $title, $remark) {
            $this->refresh();
            if ($money > $this->money) {
                throw new StandardException('提现金额大于余额');
            }
            return DB::transaction(function () use($money, $from, $title, $remark) {
                $this->money += $money;
                $this->outlay -= $money;
                $this->save();
                AccountDetail::query()->create([
                    "user_id" => $this->id,
                    "title" => $title,
                    "from" => $from,
                    "to" => AccountDetail::MAIN_BODY_ACCOUNT,
                    "money" => $money,
                    "account" => $this->money,
                    "remark" => $remark,
                ]);
            });
        });
    }

    public function getLock($callback)
    {
        Lock::lock("account:{$this->id}", 2, $callback);
    }
}
